package hht.dragon.blog.controller;

import com.github.pagehelper.PageInfo;
import hht.dragon.blog.common.api.ReturnData;
import hht.dragon.blog.common.api.utils.Returns;
import hht.dragon.blog.model.Article;
import hht.dragon.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 文章操作Controller.
 * User: huang
 * Date: 18-6-23
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService service;

    @GetMapping("/{articleId}")
    public Mono<ReturnData<Article>> getArticle(@PathVariable("articleId") int articleId) {
        Article article = service.getArticleById(articleId);
        return Returns.newMonoReturnData(200, article);
    }

    /**
     * 增加点赞数.
     * @param articleId 文章id
     * @return
     */
    @GetMapping("/{articleId}/like")
    public ReturnData<Boolean> upLike(@PathVariable("articleId") int articleId) {
        boolean ok = service.upLike(articleId);
        return Returns.newReturnData(200, ok);
    }

    /**
     * 搜索文章.
     * @param pageNum 页码
     * @param search 搜索关键字
     * @return
     */
    @GetMapping("/search/{pageNum}")
    public Mono<ReturnData<PageInfo<Article>>> searchArticle(@PathVariable("pageNum") int pageNum, String search) {
        PageInfo<Article> articles = service.pageSearchArticle(search, pageNum);
        return Returns.newMonoReturnData(200, articles);
    }

}
